package com.yanqu.road.server.cross3server.timer;

import com.yanqu.road.logic.bussiness.servercenter.ServerBussiness;
import com.yanqu.road.pb.cross3.server.CrossChannelServerProto;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.cross3server.manager.Cross3XServerNameMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ChannelProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.ConfigHelper;
import com.yanqu.road.utils.executor.GeneralTask;

import java.util.Map;

public class Cross3XServerNameTask extends GeneralTask {

    private int syncType = 0;

    @Override
    public void doRun() {

        if (syncType++ % 2 == 0) {

            // 读库
            int channelId = ConfigHelper.getInt("channelId");
            Map<Long, String> nameMap = ServerBussiness.getServerNameMap(channelId);
            for (Map.Entry<Long, String> entry : nameMap.entrySet()) {
                Cross3XServerNameMgr.getServerNameMap().addServerName(channelId, entry.getKey(), entry.getValue());
            }

            // 上传
            CrossChannelServerProto.ServerNameListMsg.Builder builder = Cross3XServerNameMgr.getServerNameMap().getServerNameMsg(channelId);
            YanQuMessage message = YanQuMessageUtils.buildMessage(ChannelProtocol.N_UPLOAD_SERVER_NAME, builder);
            MessageHelper.sendChannelServer(message);

        } else {

            // 同步
            CrossChannelServerProto.ServerNameListMsg.Builder builder = Cross3XServerNameMgr.getServerNameMap().getAllServerNameMsg();
            YanQuMessage message = YanQuMessageUtils.buildMessage(GameProtocol.S_SYNC_SERVER_NAME_FROM_CROSS3, builder);
            MessageHelper.sendAllServer(message);

        }

    }
}
